/* ---------------------------------------------------------------------------
   (c) Copyright 2006 ActivIdentity, Inc.
   All Rights Reserved.

   This program is an unpublished copyrighted work which is proprietary
   to ActivIdentity. This computer program includes Confidential,
   Proprietary Information and is a Trade Secret of ActivIdentity.
   Any use, disclosure, modification and/or reproduction is prohibited
   unless authorized in writing by ActivIdentity.

   WARNING:  Unauthorized reproduction of this program as well as
   unauthorized preparation of derivative works based upon the
   program or distribution of copies by sale, rental, lease or
   lending are violations of federal copyright laws and state trade
   secret laws, punishable by civil and criminal penalties.
   --------------------------------------------------------------------------- */

/* ---------------------------------------------------------------------------
   Disclaimer

   ACTIVIDENTITY MAKES NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE 
   CONTENTS OR USE OF THIS SOFTWARE, AND SPECIFICALLY DISCLAIMS ANY EXPRESS
   OR IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR
   PURPOSE. Further, ActivIdentity reserves the right to revise this software
   and to make changes to its content, at any time, without obligation to
   notify any person or entity of such revisions or changes.
   --------------------------------------------------------------------------- */

#ifndef __SDBSET_H__
#define __SDBSET_H__

#ifdef __cplusplus
extern "C" {
#endif

#if defined(WIN32)
/* Windows standard files and definitions */
#include <windows.h>
#include <stdio.h>
#else
/* Unix standard files and definitions */
#include <stdio.h>
#define CALLBACK
#define TRUE 1
#define FALSE 0
#endif


#include "sdbapi.h"

/***************/
/** CONSTANTS **/
/***************/
#define SDBSET_HEADER_MAGIC_NUMBER			0x53444253
#define SDBSET_TRAILER_MAGIC_NUMBER			0x454F5353

#define SDBSET_MODE_READFILE	1
#define SDBSET_MODE_WRITEFILE	2

#define SDBSET_HEADER_ENCRYPTED_SESSION_KEY_SIZE    64
#define SDBSET_HEADER_RESERVED_SIZE                 181
#define SDBSET_TRAILER_GLOBAL_MAC_SIZE              20
#define SDBSET_TRAILER_RESERVED_SIZE                36
#define SDBSET_WORKING_BUFFER_SIZE					128

#define SDBSET_GLOBAL_MAC_SCOPE_WHOLE_SDB						1
#define SDBSET_GLOBAL_MAC_SCOPE_MAC_OF_MAC						2
#define SDBSET_GLOBAL_MAC_SCOPE_WHOLE_SDB_AND_GLOBAL_HEADER		3
#define SDBSET_GLOBAL_MAC_SCOPE_MAC_OF_MAC_AND_GLOBAL_HEADER	4

#define SDBSET_HEADER_VERSION		0x02

/************/
/** MACROS **/
/************/


/**********************/
/** TYPE DEFINITIONS **/
/**********************/

typedef struct {

    unsigned int	Magic;
    unsigned char	Version;
    unsigned char	GlobalMACType;
    unsigned char	GlobalMACScope;
    int				EncryptionKeyID;
    unsigned char	EncryptedSessionKey[SDBSET_HEADER_ENCRYPTED_SESSION_KEY_SIZE];
    unsigned char	Reserved[SDBSET_HEADER_RESERVED_SIZE];

} SDBSETHeader;

typedef struct {

    unsigned int	Magic;
    unsigned int	Number;
    unsigned char	MAC[SDBSET_TRAILER_GLOBAL_MAC_SIZE];
    unsigned char	Reserved[SDBSET_TRAILER_RESERVED_SIZE];

} SDBSETTrailer;

typedef struct {
	char			*pszAsciiMACKey;
	SDBSETHeader	Header;
	SDBSETTrailer	Trailer;
	unsigned char	WorkingBuffer[SDBSET_WORKING_BUFFER_SIZE];

} SDBSETParameters;


/***************/
/** FUNCTIONS **/
/***************/

int CALLBACK SDBSETGetVersion(
        OUT char *pszVersion,
        OUT unsigned int *pVersion,
        OUT char *pszCopyright, 
        OUT char *pszComment);

/* Core functions */
int CALLBACK SDBSETCheckAdd(
		INOUT SDBSETParameters *psParameters,
        IN const PSDB pSDB);

int CALLBACK SDBSETCheckFinish(
		INOUT SDBSETParameters *psParameters);

int CALLBACK SDBSETSDBFree(INOUT PSDB pSdb);


/* File oriented functions */
int CALLBACK SDBSETOpenFile(
		INOUT SDBSETParameters *psParameters,
        IN const char *pszFileName,
		IN int nFileMode,
		OUT FILE **ppFile);

int CALLBACK SDBSETCloseFile(
        INOUT SDBSETParameters *psParameters,
		IN int nFileMode,
        INOUT FILE **ppFile);

int CALLBACK SDBSETWriteFileNext(
		INOUT SDBSETParameters *psParameters,
        INOUT FILE *pFile,
        IN const PSDB pSDB);

int CALLBACK SDBSETReadFileNext(
		INOUT SDBSETParameters *psParameters,
        INOUT FILE *pFile,
        OUT PSDB pSDB);

#ifdef __cplusplus
}
#endif

#endif /* __SDBSET_H__ */
